-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Enhancement] Support Generated Column rewrite in complex Query #50398
Conversation
|
||
GeneratedColumnVisitor visitor = new GeneratedColumnVisitor(); | ||
visitor.process(node, viewScope); | ||
|
||
return viewScope; | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How can I help you today?
public Map<Expr, SlotRef> getGeneratedExprToColumnRef() { | ||
return generatedExprToColumnRef; | ||
} | ||
|
||
@Override | ||
public <R, C> R accept(AstVisitor<R, C> visitor, C context) { | ||
return visitor.visitRelation(this, context); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The most risky bug in this code is:
Potential thread-safety issues with generatedExprToColumnRef
.
You can modify the code like this:
import java.util.Collections;
import java.util.concurrent.ConcurrentHashMap;
//...
private Map<Expr, SlotRef> generatedExprToColumnRef = new ConcurrentHashMap<>();
public void setGeneratedExprToColumnRef(Map<Expr, SlotRef> generatedExprToColumnRef) {
this.generatedExprToColumnRef = Collections.synchronizedMap(generatedExprToColumnRef);
}
784e377
to
7583580
Compare
f8b5ab8
to
05deed6
Compare
In this pr: 1. Refactor Generated Column rewrite code: a. Collect generated column expr mapping From QueryAnalyzer. b. Introduce ReplaceScalarOperatorRule to replace the ScalarOperator match the generated column expression. 2. Supprot View, JOIN with Subquery and CTE for Generated Column rewriting. 3. Support simple equivalent expression rewrite for generated column. Note: All query structure support Generated Column rewriting obey a simple rule: the Generated Column and its ref columns output directly into the outer scope. Signed-off-by: srlch <[email protected]>
f145e83
to
3068c59
Compare
Quality Gate passedIssues Measures |
[Java-Extensions Incremental Coverage Report]✅ pass : 0 / 0 (0%) |
[FE Incremental Coverage Report]✅ pass : 127 / 132 (96.21%) file detail
|
[BE Incremental Coverage Report]✅ pass : 0 / 0 (0%) |
@Mergifyio backport branch-3.3 |
✅ Backports have been created
|
Signed-off-by: srlch <[email protected]> (cherry picked from commit 96b97a7) # Conflicts: # fe/fe-core/src/main/java/com/starrocks/sql/analyzer/QueryAnalyzer.java
…Rocks#50398) Signed-off-by: srlch <[email protected]>
…Rocks#50398) Signed-off-by: srlch <[email protected]>
…Rocks#50398) Signed-off-by: srlch <[email protected]>
…port StarRocks#50398) Signed-off-by: srlch <[email protected]>
…port #50398) (#50830) Signed-off-by: srlch <[email protected]>
…port #50398) (#50831) Signed-off-by: srlch <[email protected]>
…Rocks#50398) Signed-off-by: srlch <[email protected]>
Why I'm doing: This problem is introduce by pr #StarRocks#50398. When we re-analyze the generated expression in current scope for subquery, sometime we may meet an exception. In current implementation, a map will be clear but the iteration does not break cause ConcurrentModificationException. What I'm doing: Remove the expression if re-analyze throw an exception instead of clear all generated column expression. Signed-off-by: srlch <[email protected]>
…relation if left table and right table has the same column name Why I'm doing: This problem is introduced by pr StarRocks#50398. In this pr, we introduce some new rule for generated column rewriting. The basic idea is following: 1. Collect the rewriting relation in every SELECT scope in query. (Expr -> ColumnRef) 2. Translate the expression relation into Operator mapping: ScalarOperator -> ColumnRefOperator 3. Introduce new rule says ReplaceScalarOperatorRule, use this new rule to replace the ScalarOperator into ColumnRefOperator when generating the logical plan in optimizer. This problem is that, ReplaceScalarOperatorRule use ScalarOperator.isEquivalent to check if a ScalarOperator is hit the rule instead of using ScalarOperator.equals. ScalarOperator.isEquivalent does not check the operator id but this id will be used to indentify the column with the same column name but come from different table in JOIN relation. (e.g column xx in TABLE A and column xx in TABLE B has same name but different id, in this case, ScalarOperator.isEquivalent return true but ScalarOperator.equals return false). So in this case, we will get the wrong mapping and generated a incorrent plan for generated column rewrite. What I'm doing: 1. Using ScalarOperator.equals instead 2. Introduce session variables disable_generated_column_rewrite for disable the generated column rewrite if we want. Signed-off-by: srlch <[email protected]>
…relation if left table and right table has the same column name Why I'm doing: This problem is introduced by pr StarRocks#50398. In this pr, we introduce some new rule for generated column rewriting. The basic idea is following: 1. Collect the rewriting relation in every SELECT scope in query. (Expr -> ColumnRef) 2. Translate the expression relation into Operator mapping: ScalarOperator -> ColumnRefOperator 3. Introduce new rule says ReplaceScalarOperatorRule, use this new rule to replace the ScalarOperator into ColumnRefOperator when generating the logical plan in optimizer. This problem is that, ReplaceScalarOperatorRule use ScalarOperator.isEquivalent to check if a ScalarOperator is hit the rule instead of using ScalarOperator.equals. ScalarOperator.isEquivalent does not check the operator id but this id will be used to indentify the column with the same column name but come from different table in JOIN relation. (e.g column xx in TABLE A and column xx in TABLE B has same name but different id, in this case, ScalarOperator.isEquivalent return true but ScalarOperator.equals return false). So in this case, we will get the wrong mapping and generated a incorrent plan for generated column rewrite. What I'm doing: 1. Using ScalarOperator.equals instead 2. Introduce session variables disable_generated_column_rewrite for disable the generated column rewrite if we want. Signed-off-by: srlch <[email protected]>
…Rocks#50398) Signed-off-by: srlch <[email protected]> Signed-off-by: zhiminr.ren <[email protected]>
In this pr:
a. Collect generated column expr mapping From QueryAnalyzer.
b. Introduce ReplaceScalarOperatorRule to replace the ScalarOperator match the generated column expression.
Note: All query structure support Generated Column rewriting obey a simple rule: the Generated Column and its ref columns output directly into the outer scope.
Fixes #issue
What type of PR is this:
Does this PR entail a change in behavior?
If yes, please specify the type of change:
Checklist:
Bugfix cherry-pick branch check: